package io.finer.erp.jeecg.stock.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.finer.erp.jeecg.purchase.entity.PurchaseRequest;
import io.finer.erp.jeecg.stock.entity.StkIoBill;
import io.finer.erp.jeecg.stock.service.StockMoveService;
import io.finer.erp.jeecg.stock.vo.PurchaseStockMove;
import io.finer.erp.jeecg.stock.vo.StockMovePage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 库存移动 前端控制器
 * </p>
 *
 * @author yaoyifan
 * @since 2021-05-30
 */
@Api(tags="库存移动")
@RestController
@RequestMapping("/stock/stockMove")
@Slf4j
public class StockMoveController {

    @Autowired
    StockMoveService stockMoveService;

    @AutoLog(value="库存移动-分页列表查询")
    @ApiOperation(value="库存移动-分页列表查询", notes="库存移动-分页列表查询")
    @GetMapping("/list")
    public Result<?> queryPageList(StockMovePage stockMovePage,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<StockMovePage> queryWrapper = QueryGenerator.initQueryWrapper(stockMovePage, req.getParameterMap());
        Page<StockMovePage> page = new Page<>(pageNo, pageSize);
        IPage<StockMovePage> pageList = stockMoveService.selectStockMovePage(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "库存移动-通过id查询")
    @ApiOperation(value="库存移动-通过id查询", notes="库存移动-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        StockMovePage stockMovePage = stockMoveService.selectStockMovePageById(id);
        if(stockMovePage==null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(stockMovePage);

    }

    @AutoLog(value = "库存移动-采购入库-通过ids查询")
    @ApiOperation(value="库存移动-采购入库-通过ids查询", notes="库存移动-采购入库-通过ids查询")
    @GetMapping(value = "/queryStockMoveByIds")
    public Result<?> queryPurchaseStockMoveByIds(@RequestParam(name="ids",required=true) String ids) {
        List<String> id_list = Arrays.asList(ids.split(","));
        Page<StockMovePage> page = new Page<>(1, 10000);
        QueryWrapper<StockMovePage> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",id_list);
        IPage<StockMovePage> stockMovePageList = stockMoveService.selectStockMovePage(page, queryWrapper);
        return Result.ok(stockMovePageList);
    }

    @AutoLog(value="库存移动-采购入库-分页列表查询")
    @ApiOperation(value="库存移动-采购入库-分页列表查询", notes="库存移动-采购入库-分页列表查询")
    @GetMapping("/list/purchaseStockMove")
    public Result<?> queryPageListPurchaseStockMove(StockMovePage stockMovePage,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<StockMovePage> queryWrapper = QueryGenerator.initQueryWrapper(stockMovePage, req.getParameterMap());
        Page<StockMovePage> page = new Page<>(pageNo, pageSize);
        queryWrapper.eq("transaction_type", "采购入库").eq("state","可用");
        IPage<StockMovePage> pageList = stockMoveService.selectStockMovePage(page, queryWrapper);
        return Result.ok(pageList);
    }

    @AutoLog(value = "库存移动-采购入库处理")
    @ApiOperation(value="库存移动-采购入库处理", notes="库存移动-采购入库处理")
    @PostMapping(value = "/purchaseStockMoveIn")
    public Result<?> purchaseStockMoveIn(@RequestBody List<PurchaseStockMove> purchaseStockMoveList) {
        stockMoveService.purchaseStockMoveIn(purchaseStockMoveList);
        return Result.ok("采购入库成功！");
    }
}

